<?php
header("Content-Type: text/html; charset=iso-8859-1");
require_once('include.php');
if(!check_admin('commission_presse')) redirect('perso.php?action=admin');
else{
	$_SESSION['navigation'] = 'historique_admin';
	$action = (!empty($_REQUEST['action'])) ? $_REQUEST['action'] : "";
	$id = (!empty($_REQUEST['id'])) ? $_REQUEST['id'] : "";
	$_SESSION['id'] = $id;
	$_SESSION['cat'] = 'historique';
	$user_id = check_cookie();
	if($action == 'del'){
		if($id){
			$historique = new historique($id);
			echo ($historique->delete()) ? "historique effac&eacute;e" : "D&eacute;sol&eacute;, une erreur est survenue";
		}
	}
	elseif($action == 'new'){
		echo ($id) ? "<h1>Modifier l'article</h1>" : "<h1>Nouvel article</h1>\n";
		$user_id = check_cookie();
		if($id){
			$historique = new historique($id);
			$tab_date = explode("-",$historique->date);
			$jour= $tab_date[2];
			$mois = $tab_date[1];
			$annee = $tab_date[0];
		}
		else{
			$jour = date('d');
			$mois = date('m');
			$annee = date("Y");
		}
		echo "<form action = 'historique_admin.php' method = 'post' id = 'myform' name = 'myform'>\n";
			echo leftCol("Titre: ",10);
			$value_titre = ($id) ? $historique->titre : "";
			echo rightCol("<input type = 'text' name = 'titre' size = '40' onkeyup = 'writediv(this.value,\"titre_hist\")' onblur = 'writediv(this.value,\"titre_hist\")' value = \"$value_titre\" />",88);
			echo leftCol("Journal: ",10);
			$value_journal = ($id) ? $historique->journal : "";
			echo rightCol("<input type = 'text' name = 'journal' size = '40' onkeyup = 'writediv(this.value,\"journal_hist\")' onblur = 'writediv(this.value,\"journal_hist\")' value = \"$value_journal\" />",88);
			echo leftCol("Date: ",10);
			$print_date = "<input type = 'text' name = 'jour' size = '2' maxlength = '2' onkeyup = 'writediv(document.myform.jour.value+\".\"+document.myform.mois.value+\".\"+document.myform.annee.value,\"journal_date\")' value = '$jour' />.";
			$print_date .= "<input type = 'text' name = 'mois' size = '2' maxlength = '2' onkeyup = 'writediv(document.myform.jour.value+\".\"+document.myform.mois.value+\".\"+document.myform.annee.value,\"journal_date\")' value = '$mois' />.";
			$print_date .= "<input type = 'text' name = 'annee' size = '4' maxlength = '4' onkeyup = 'writediv(document.myform.jour.value+\".\"+document.myform.mois.value+\".\"+document.myform.annee.value,\"journal_date\")' value = '$annee' />";
			echo rightCol($print_date,88);
			$content = "<button type = 'button' name = 'citation' onclick = 'inserttag(\"_\",document.myform.corps);preview(document.myform.corps.value,\"corps_hist\");'>_italique_</button>\n";
			$content .= "<button type = 'button' name = 'citation' onclick = 'inserttag(\"*\",document.myform.corps);preview(document.myform.corps.value,\"corps_hist\");'>*gras*</button>\n";
			$content .= "<button type = 'button' name = 'couleur' onclick = 'inserttag(\"!!\",document.myform.corps);preview(document.myform.corps.value,\"corps_hist\");'>!!couleur!!</button>\n";
			$content .= "<button type = 'button' name = 'citation' onclick = 'insertcode(\" &#39;&#39;Titre&#39;&#39;:http://adresse \",document.myform.corps);preview(document.myform.corps.value,\"corps_hist\");'>lien</button>\n";
			echo leftCol("",10);
			echo rightCol($content,88);
			echo leftCol("Corps: ",10);
			$value_corps = ($id) ? $historique->corps : "";
			echo rightCol("<textarea name = 'corps' cols = '40' rows = '20' onkeyup = 'preview(this.value,\"corps_hist\")' onblur = 'preview(this.value,\"corps_hist\")'>$value_corps</textarea>",88);
			echo leftCol("Publi&eacute;e",10);
			$checked = ($id) ? (($historique->valide) ? "checked = 'checked'" : "") : "";
			echo rightCol("<input type = 'checkbox' name = 'valide' value = '1' $checked />",88);
			echo leftCol("",10);
			$print = ($id) ? "<input type = 'hidden' name = 'id' value = '$id' />\n" : "";
			$right = "<p class  ='center'>$print";
			$right .= "<button type = 'button' onclick = 'list_historique()'>annuler</button>&nbsp;\n";
			$right .= "<input type = 'hidden' name = 'action' value = 'add' />\n";
			$right .= "<button type = 'submit' name = 'submit' value = 'add'>enregistrer</button></p>\n";
			echo rightCol($right,88);
			echo empty_line_two_col();
		echo "</form>\n";
		echo "<div style = 'position: absolute; top: 50px; right: 0px' id = 'iframe'>\n";
		echo "    <iframe src='img_upload.php?div=hist' frameborder='' scrolling='0'></iframe>\n";
		echo "</div>\n";
		echo "<div id='list'></div>\n";
		echo "<div class = 'historique'>\n";
		echo "<div class = 'info_title' id = 'titre_hist'>$value_titre</div>\n";
		echo "<div class = 'info_journal'><span id = 'journal_hist'>$value_journal</span><span class = 'info_date' id = 'journal_date'>$jour.$mois.$annee</span></div>\n";
		echo "<div class = 'info_corps preview' id = 'corps_hist'>".(prepare_info($value_corps))."</div>\n";
		echo "</div>\n";
	}
	elseif($action == 'resume'){
		$periode = (!empty($_REQUEST['periode'])) ? $_REQUEST['periode'] : "";
		$titre = ($periode) ? "r&eacute;sum&eacute; de ".$periode : "R&eacute;sum&eacute; d&#39;une p&eacute;riode";
		echo "<h1>$titre</h1>\n";
		echo "<form action = 'javascript:update_presse(\"$periode\",document.myform.corps.value)' method = 'post' name = 'myform' id = 'myform'>\n";
		$content = "<select name = 'periode' onchange = 'if(this.options[this.selectedIndex].value) resume_periode(this.options[this.selectedIndex].value)'>";
		$selected = ($periode) ? "" : "selected = 'selected'";
		$content .= "<option value = '' $selected>-- choisir une p&eacute;riode --</option>\n";
		$end = (date('m') >= 7) ? date('Y')+1 : date('Y');
		for($i = 1976; $i < $end; $i++){
			$print = $i."-".($i+1);
			$selected = ($print == $periode) ? "selected = 'selected'" : "";
			$content .= "<option value= '$print' $selected>$print</option>\n";
		}
		$content .= "</select>\n";
		echo leftCol("<span class = 'bold'>p&eacute;riode</span>",10);
		echo rightCol($content,88);
		if($periode){
			$content = "<button type = 'button' name = 'citation' onclick = 'inserttag(\"_\",document.myform.corps);preview(document.myform.corps.value,\"corps_presse\");'>_italique_</button>\n";
			$content .= "<button type = 'button' name = 'citation' onclick = 'inserttag(\"*\",document.myform.corps);preview(document.myform.corps.value,\"corps_presse\");'>*gras*</button>\n";
			$content .= "<button type = 'button' name = 'couleur' onclick = 'inserttag(\"!!\",document.myform.corps);preview(document.myform.corps.value,\"corps_presse\");'>!!couleur!!</button>\n";
			$content .= "<button type = 'button' name = 'citation' onclick = 'insertcode(\" &#39;&#39;Titre&#39;&#39;:http://adresse \",document.myform.corps);preview(document.myform.corps.value,\"corps\");'>lien</button>\n";
			echo leftCol("",10);
			echo rightCol($content,88);
			$q = "select description from presse where periode = '$periode'";
			$r = ExecRequete($q);
			$value_corps = ($s = LigneSuivante($r)) ? $s->description : "";
			echo leftCol("<span class = 'bold'>r&eacute;sum&eacute;</span>",10);
			echo rightCol("<textarea id = 'corps' name = 'corps' cols = '50' rows = '20' onkeyup = 'preview(this.value,\"corps_presse\")' onblur = 'preview(this.value,\"corps_presse\")'>$value_corps</textarea>",88);
			echo "<br class = 'clear' />\n";
			echo "<div class = 'info_corps preview' id = 'corps_presse'>".(prepare_info($value_corps))."</div>\n";
			echo "<p class = 'center'><input type = 'hidden' name = 'periode' value = '$periode' />\n";
			echo "<button type = 'button' onclick = 'list_historique()' >&larr;&nbsp;retour</button>\n";
			echo "&nbsp;<button type = 'submit' name ='action' value = 'update_presse'><img src = 'images/icon_ok.png' alt = '' height = '14' width = '14' />enregistrer</button>\n";
			echo "&nbsp;<button type = 'button' onclick = 'resume_periode(\"$periode\")' >annuler</button>\n";
			echo "</p>\n";
		}
		else echo "<br class = 'clear' />\n";
		echo "</form>\n";
	}
	elseif($action == 'update_presse'){
		$periode = (!empty($_REQUEST['periode'])) ? $_REQUEST['periode'] : "";
		$corps = (!empty($_REQUEST['corps'])) ? $_REQUEST['corps'] : "";
		if($periode){
			$q = "replace presse set periode = '".(addslashes($periode))."', description = '".(addslashes($corps))."'";
			$echo = (ExecRequete($q)) ? 1 : 0;
			echo $echo;
		}
	}
	elseif($action == 'list'){
		echo "<h1>L'historique</h1>\n";
		$q = "select id from historique order by date desc";
		$r = ExecRequete($q);
		$max = mysql_num_rows($r);
		$i = 0;
		$fields = array("id","date","titre","journal","valide",'actions');
		foreach($fields as $field){
			$$field = (!empty($_REQUEST[$field])) ? $_REQUEST[$field] : "";
		}
		echo "<p class = 'center'><button type = 'button' onclick = 'new_historique()'><img src = 'images/icon_new.png' alt = '' width = '14' height = '14' />nouvel article</button>&nbsp;<button type = 'button' onclick = 'resume_periode()'><img src = 'images/icon_txt.png' alt = '' width = '14' height = '14' />r&eacute;sum&eacute; d&#39;une p&eacute;riode</button></p>\n";
		$table = new table();
		foreach($fields as $field){
			$$field = new table_field($field);
			$$field->print = $field;
			if($field != 'titre' && $field != 'journal') $$field->styleclass = 'center';
			$table->fields[] = $$field;
		}
		$valide->filter = 0;
		$table->sense = "DESC";
		$table->order =  "date";
		$table->styleclass = '';
		$table->filter = 1;

		while($s = LigneSuivante($r)){
			$historique = new historique($s->id);
			$row = new table_row();
			foreach($fields as $field){
				if($field == 'titre'){
					$print = (strlen($historique->$field) > 40) ? substr($historique->$field,0,40)."..." : $historique->$field;
				}
				#elseif($field == 'date') $print = format_date($historique->date);
				elseif($field == 'valide') $print = ($historique->$field) ? "<span class = 'oui'>oui</span>" : "<span class = 'non'>non</span>";
				elseif($field == 'actions'){
					$print =  "<img src = 'images/icon_edit.png' alt = 'edit' height = '14' width = '14' style = 'cursor:pointer' onclick = 'edit_historique($historique->id)'\"/>";
					$print .=  "&nbsp;<img src = 'images/icon_delete.png' alt = 'del' height = '14' width = '14' style = 'cursor:pointer' onclick = 'if(confirm(\"Confirmer la suppression de l historique\"))del_historique($historique->id)' />";
				}
				else $print = $historique->$field;
				$row->cols[$field] = $print;
			}
			$table->rows[] = $row;
		}
		echo $table->print_header();
		echo $table->print_body();
		echo $table->print_footer();
	}
	else{
		entete('VBCLL - Bienvenue',"","",1);
		if($action == 'add'){
			$fields = array('corps','titre','jour','mois','annee','journal','valide');
			foreach($fields as $field) $$field = (!empty($_REQUEST[$field])) ? $_REQUEST[$field] : "";
			$date = ($annee && $mois && $jour) ? $annee."-".$mois."-".$jour : date('Y-m-d');
	        $tab_date = explode("-",$date);
			$periode = ($tab_date[1]>7) ? $tab_date[0]."-".($tab_date[0]+1) :( $tab_date[0]-1)."-".$tab_date[0];
			$new_historique = ($id) ? new historique($id) : new historique('new');
			$new_historique->corps = $corps;
			$new_historique->titre = $titre;
			$new_historique->journal = $journal;
			$new_historique->date = $date;
			$new_historique->periode = $periode;
			$new_historique->valide = $valide;
			if($id){
				if($new_historique->update()){
					$texte = "mise &agrave; jour r&eacute;ussie";
				}
				else $texte = "D&eacute;sol&eacute;, une erreur s'est produite";
			}
			else{
				$result = $new_historique->insert();
				if($result){
					$texte = $result;
					unset($_SESSION['rand']);
				}
				else $texte = "D&eacute;sol&eacute;, une erreur s'est produite";
			}
			echo "<script type = 'text/javascript'>message(\"$texte\")</script>";
		}
		echo "<div id = 'action'></div>\n";
		echo "<script type = 'text/javascript'>list_historique(1)</script>\n";
		pied();
	}
}
